<template>
  <div class="article">
    <el-card>
      <template #header>
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="'/home'">首页</el-breadcrumb-item>
          <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        </el-breadcrumb>
      </template>
      <el-form label-width="100px">
        <el-form-item label="状态 :">
          <!-- 单选框组件 -->
          <el-radio-group v-model="form.status">
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
            <el-radio :label="4">已删除</el-radio>
          </el-radio-group>

          <!-- 频道 -->
        </el-form-item>
        <el-form-item label="频道">
          <PinDao :form="form"></PinDao>
        </el-form-item>
        <!-- 日期 -->
        <el-form-item label="日期">
          <el-date-picker
            @change="pickerChange"
            v-model="form.picker"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button @click="submit" type="primary">筛选</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <template #header>
        <div>根据筛条件共查询{{ total }}条结果</div>
      </template>
      <el-table :data="list">
        <el-table-column label="封面">
          <template v-slot="scope">
            <img class="cover" :src="scope.row.cover.images[0]" alt="" />
          </template>
        </el-table-column>
        <el-table-column label="标题" prop="title"></el-table-column>
        <el-table-column label="状态" prop="status">
          <template v-slot="scope">
            {{ scope.row.status === 2 ? '审核通过' : '审核不通过' }}
          </template>
        </el-table-column>
        <el-table-column label="发布时间" width="170px">
          <template v-slot="{ row }">
            {{ row.pubdate }}
          </template>
        </el-table-column>
        <el-table-column label="阅读数" prop="read_count"></el-table-column>
        <el-table-column label="评论数" prop="comment_count"></el-table-column>
        <el-table-column label="点赞数" prop="like_count"></el-table-column>
        <el-table-column label="操作" width="300">
          <template v-slot="scope">
            <div>
              <!-- <h3>{{ scope.$index }}</h3> -->
              <el-button @click="edit(scope.row)">编辑</el-button>
              <el-button @click="del(scope.row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :total="total"
        :page-size="form.per_page"
        :page-sizes="[1, 10, 20, 30, 40]"
        :current-page="form.page"
        layout="total,sizes,prev,pager,next,jumper"
        @current-change="pageChange"
        @size-change="sizeChange"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import { channels, mpArticles, mpArticlesDelete } from '@/api/index.js'
export default {
  data () {
    return {
      form: {
        picker: [], //[起始时间，结束时间]，
        status: null,
        channel_id: '',
        begin_pubdate: '',
        end_pudbate: '',
        page: 1,
        per_page: 10
      },
      channelsList: [],
      list: [],
      total: 0
    }
  },

  async created () {
    this.getDate()
  },
  methods: {
    pickerChange (value) {
      // console.log(value)
      if (value.length === 2) {
        this.form.begin_pubdate = value[0]
        this.form.end_pudbate = value[1]
      } else {
        this.form.begin_pubdate = value[null]
        this.form.end_pudbate = value[null]
      }
    },

    //点击搜索 1：点击搜索时回到第一页 2：也容器改变时会到第一页
    async submit () {
      // console.log('文章列表', res)
      this.form.page = 1 //搜索时回到第一页
      this.getDate()
    },
    async getDate () {
      const res = await mpArticles()
      this.list = res.data.results
      this.total = res.data.total_count
      // console.log('文章列表', res)
    },
    // 页码修改
    pageChange (page) {
      this.form.page = page //页码改变 调用接口
      this.getDate()
    },
    sizeChange (size) {
      this.form.per_page = size
      // 页码回到第一页
      this.form.page = 1 // 页容器改变回到第一页
      this.getDate()
    },
    del (row) {
      this.$confirm('你确定删除该条数据吗?', '温馨提示', {
        type: 'warning',
        confirmButtonText: '一定要删除',
        cancelButtonText: '点错了'
      })
        .then(async () => {
          // console.log('确认')
          // 调用删除api传id
          await mpArticlesDelete(row.id)
          // 提示
          this.$message({ type: 'success', message: '删除成功!' })
          // 刷新列表到数据到第一页
          this.submit()
        })
        .catch(() => {
          // console.log('取消')
          this.$message({ type: 'info', message: '已取消删除' })
        })
    },
    // 编辑
    edit (row) {
      this.$router.push('/publish?id=' + row.id)
    }
  }
}
</script>

<style lang="less" scoped>
.cover {
  width: 50px;
}
</style>
